#include "Enter.h"
#include "ui_enter.h"
#include <QDebug>
#include <QMessageBox>
#include <QCloseEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QStackedWidget>



enter::enter(QWidget *parent):QDialog(parent), ui(new Ui::enter)
{
    net=NetData::GetNetWork();
    ui->setupUi(this);
    QIcon icon(":/image/ico");
    setWindowTitle("登录系统");
    icon.pixmap(QSize(50,50));
    setWindowIcon(icon);
    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_2->setCurrentIndex(0);
    ui->pushButton->setIcon(QIcon(":/image/cancel_password"));
    ui->password->setEchoMode(QLineEdit::Password);
    CodeCount = std::make_shared<QTimer>();
    //set windows color

    //设置登录按钮和注册按钮
    bnt=std::make_shared<Round_Button>(ui->widget_2);
    bnt->setColor(QColor(180,0,0,200));
    bnt->setPressColor(QColor(200,0,0,100));
    QPen pen(QColor(0,0,0,255));
    bnt->setTxet("登录");
    bnt->setTxetFontAndColor(QFont("HONOR Sans Text GBK"),QColor(240,240,240,255));
    bnt->setGeometry(125,35,200,40);

    gbnt= std::make_shared<Round_Button>(ui->widget_3);
    gbnt->setColor(QColor(180,0,0,200));
    gbnt->setPressColor(QColor(200,0,0,100));
    QPen gpen(QColor(0,0,0,255));
    gbnt->setTxet("注册");
    gbnt->setTxetFontAndColor(QFont("HONOR Sans Text GBK"),QColor(240,240,240,255));
    gbnt->setGeometry(115,15,200,40);

    ui->code_label->hide();

    //set tips text
    ui->account->setPlaceholderText("请输入账号/昵称");
    ui->password->setPlaceholderText("请输入密码");
    ui->PhonNuber->setPlaceholderText("请输入手机号");
    ui->ChiekCode->setPlaceholderText("请输入验证码");
    //set eidt no frame
    ui->account->setFrame(false);
    ui->password->setFrame(false);
    ui->PhonNuber->setFrame(false);
    ui->ChiekCode->setFrame(false);    
    mainwin=nullptr;

    connect(ui->phoebtn,&Mouset::entered,this,[=](){
       ui->phoebtn->setStyleSheet("color:rgb(255,80,0);background:transparent;");
    });
    connect(ui->phoebtn,&Mouset::leaved,this,[=](){
       ui->phoebtn->setStyleSheet("color:rgb(0,0,0);background:transparent;");
    });
    connect(ui->accountbtn,&Mouset::entered,this,[=](){
       ui->accountbtn->setStyleSheet("color:rgb(255,80,0);background:transparent;");
    });
    connect(ui->accountbtn,&Mouset::leaved,this,[=](){
       ui->accountbtn->setStyleSheet("color:rgb(0,0,0);background:transparent;");
    });

    //窗口/网络槽

   //密码提交后服务器反馈槽
   connect(net,&NetData::posted,this,[=](bool ok){
             if(ok){
                // this->hide();
                 mainwin->show();
             }else{QMessageBox::information(this,"密码错误","输入密码错误请重新输入");}
   });

   //账号注册后服务器反馈槽
   connect(net,&NetData::registed,this,[this](bool ok){
        if(ok)
        {
            QMessageBox::information(this,"注册","注册成功");
        }else{
            QMessageBox::information(this,"注册","注册失败");
        }
   });

   connect(CodeCount.get(),&QTimer::timeout,this,[&](){
     --codes;
    if(codes<=0)
    {
       ui->code_label->setText("");
       ui->code_label->hide();
       ui->pushButton_2->show();
       codes=60;
       CodeCount->stop();
       return;
    }

   QString tips = QString("重新发送\n %1 秒").arg(codes);
     ui->code_label->setText(tips);

   });

    //注册确认点击事件
   connect(gbnt.get(),&Round_Button::m_clicked,this,&enter::certain_clicked);
    //登录确认点击事件
   connect(bnt.get(),&Round_Button::m_clicked,this,&enter::enterbtn_clicked);



   reg_acount_label=new QLabel(ui->widget_5);
   reg_passwd_label=new QLabel(ui->widget_4);

   QFont regfont = QFont("HONOR Sans Text GBK");
   reg_acount_label->setFont(regfont);
   reg_passwd_label->setFont(regfont);
   reg_passwd_label->setStyleSheet("color:red");
   reg_acount_label->setStyleSheet("color:red");
   reg_acount_label->setGeometry(100,50,200,35);
   reg_passwd_label->setGeometry(100,50,200,35);

}

enter::~enter()
{
    delete ui;
}

//手机号登录页面检测
void enter::on_phoebtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->label->setStyleSheet("background-color:rgb(255,80,0);");
    ui->label_2->setStyleSheet("");
}
//账号号登录页面检测
void enter::on_accountbtn_clicked()
{
     ui->stackedWidget->setCurrentIndex(0);
     ui->label_2->setStyleSheet("background-color:rgb(255,80,0);");
      ui->label->setStyleSheet("");
}

//void enter::on_phoebtn_toggled(bool checked){}

//密码显示按钮
void enter::on_pushButton_toggled(bool checked)
{
    if(checked)
    {
     ui->pushButton->setIcon(QIcon(":/image/show_password"));

        ui->password->setEchoMode(QLineEdit::Normal);
    }else
    {
        ui->pushButton->setIcon(QIcon(":/image/cancel_password"));
        ui->password->setEchoMode(QLineEdit::Password);
    }
}

Mouset::Mouset(QWidget *parent): QPushButton(parent)
{}

Mouset::~Mouset()
{}

//鼠标进入事件
void Mouset::enterEvent(QEvent *event)
{
    emit entered();
     return ;
}
//鼠标离开事件
void Mouset::leaveEvent(QEvent *event)
{
    emit leaved();
    return ;
}

//点击登录按钮
void enter::enterbtn_clicked()
{

/* if ！ */
    if(net->IsLink())
    {
       QMessageBox::warning(this,"未连接网络","未连接服务器，请先检查网络在登录");
       return ;
    }

    if(mainwin==nullptr)
    {
         mainwin=std::make_shared<MainWindow>();
         mainwin->show();
         connect(mainwin.get(),&MainWindow::reaccount,this,[&](){
        //  qDebug()<<"call"<<endl;
         this->show();
    });

    }

    //判断登录方式
    if(ui->stackedWidget->currentIndex())
    {
   //为1使用手机号登录
       phonNuber= ui->PhonNuber->text();
       chiekCode=ui->ChiekCode->text();
        QMessageBox::information(this,"提醒","抱歉暂无开启此功能");
   }
   else{
       //为0使用账号登录
       account=ui->account->text();
       password=ui->password->text();
       net->Post(password,account);
    }
}

//切换注册界面
void enter::on_registe_btn_clicked()
{
    ui->stackedWidget_2->setCurrentIndex(1);
}

//注册确认
void enter::certain_clicked()
{
   // if(!(net->IsLink()))
   // {QMessageBox::information(this,"网络错误","未连接服务器"); return;}
    on_account_Edit_editingFinished();
    if(!isReady)
    {
        reg_acount_label->setText("账号有误");
        reg_account.clear();
        reg_passwd.clear();
        return ;
    }

    on_passwd_Edit_editingFinished();
    if(!isReady)
    {
         reg_passwd_label->setText("密码有误");
        reg_account.clear();
        reg_passwd.clear();
        return ;
    }
    QString repasswd=ui->repasswd_Edit->text();

    if(repasswd.isEmpty() || repasswd != reg_passwd)
    {
        reg_account.clear();
        reg_passwd.clear();
        reg_repasswd.clear();
        isReady = false;
        QMessageBox::information(this,"信息不全","确认密码与密码不一致");
        return;
    }

    net->Registerd(reg_account,reg_passwd);//发送给服务器注册
    reg_account.clear();
    reg_passwd.clear();
    reg_repasswd.clear();
    reg_acount_label->setText("");
    reg_passwd_label->setText("");
     isReady = false;
}


//返回登录界面
void enter::on_pushButton_3_clicked()
{
    ui->account_Edit->clear();
    ui->passwd_Edit->clear();
    ui->repasswd_Edit->clear();
    ui->stackedWidget_2->setCurrentIndex(0);
}

//账号框输入完成事件
void enter::on_account_Edit_editingFinished()
{
    isReady = false;
     reg_acount_label->setText("");

    reg_account=ui->account_Edit->text();
    if(reg_account.isEmpty() )
    {return ;}
    if(reg_account.size() < 3)
    {
           isReady = false;
            reg_acount_label->setText(" 账号长度不足！");
        return;
    }

    QString str="1234567890qwertyuiopasdfghjklzxcvbnm@#$%^&*";
    int i;
    for (i = 0; i < reg_account.length(); ++i) {
       if (!str.contains(reg_account[i]))
          {
             break; // 找到第一个不在 chars 中的字符
          }
       }

    if(i != reg_account.length())
     {
         isReady = false;
         reg_acount_label->setText(" 账号名称有误,不包含："+reg_account[i]);
         return;
     }

    isReady = true;
}

//输入密码完成检测
void enter::on_passwd_Edit_editingFinished()
{
    isReady = false;
    reg_passwd_label->setText("");
    reg_passwd=ui->passwd_Edit->text();
    if(reg_passwd.isEmpty())
    {return;}
    if(reg_passwd.size() < 6 )
    {
         isReady = false;
        reg_passwd_label->setText("密码长度不足");
        return;
    }

    QString str="1234567890qwertyuiopasdfghjklzxcvbnm.!@#$%^&*";
    int i;
    for (i = 0; i < reg_passwd.length(); ++i) {
       if (!str.contains(reg_passwd[i]))
          {
             break; // 找到第一个不在 chars 中的字符
          }
       }

    if(i != reg_passwd.length())
     {
         isReady = false;
         reg_passwd_label->setText("密码格式不支持,不包含："+reg_passwd[i]);
         return;
     }
    isReady = true;
}

//验证码发送
void enter::on_pushButton_2_clicked()
{
    //发送到服务器验证
    /**/
    ui->pushButton_2->hide();
    ui->code_label->show();

    CodeCount->start(1000);
}


